package com.dayuanit.dy17.atm.controller;

import com.dayuanit.dy17.atm.dto.FlowDTO;
import com.dayuanit.dy17.atm.dto.ResponseDTO;
import com.dayuanit.dy17.atm.entity.Card;
import com.dayuanit.dy17.atm.entity.Flow;
import com.dayuanit.dy17.atm.exception.BizException;
import com.dayuanit.dy17.atm.service.ICardService;
import com.dayuanit.dy17.atm.service.IRedisService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * Controller层 是有状态的 也就是可以知道当前请求是哪个用户发送的
 * Service层 是无状态的， 也就说Service层不知道是哪个用户调用的，也无需知道。若要知道，则把userID传过来。
 *
 * 实际开发中，用户的信息都是在Controller层获取，然后传给Service层使。
 */
@RestController
public class CardController extends BaseController implements InitializingBean {

    @Autowired
    private ICardService cardService;

    @Autowired
    @Qualifier(value = "redisServiceTmpleteImpl")
    private IRedisService redisService;

    public CardController() {
//        System.out.println("---------CardController-------------cardService =" + cardService.toString());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("---------afterPropertiesSet-------------cardService =" + cardService.toString());
    }

    @RequestMapping("/card/openAccount")
    public ResponseDTO openAccount(HttpServletRequest request, String password, String confirmPassword) {
        try {
            cardService.openAccount(getUserId(request.getSession()), password, confirmPassword);
            return ResponseDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.fail(e.getMessage());
        }
    }

    @RequestMapping(value = "/card/deposit", method = RequestMethod.POST)
    public ResponseDTO deposit(HttpSession session, long cardId, String password, String amount) {
        try {
            cardService.deposit(cardId, password, amount, getUserId(session));
            // !!!!!! 缓存的更新策略：先修改DB，生效后(也就是事务提交后)，再将对应的redis缓存置为失效。 !!!!!!!!!!
            redisService.expireKey("atm:flow:" + getUserId(session));
            return ResponseDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.fail(e.getMessage());
        }
    }

    @PostMapping("/card/transfer")
    public ResponseDTO transfer(long cardOutId, String cardInNumber,
                                String password, String amount, HttpSession session) {
        try {
//            cardService.transfer(cardOutId, cardInNumber, password, amount, getUserId(session));
            cardService.delayTransfer(cardOutId, cardInNumber, password, amount, getUserId(session));
            return ResponseDTO.success();
        } catch (BizException be) {
            //业务异常  可以把异常信息给客户端看
            be.printStackTrace();
            return ResponseDTO.fail(be.getMessage());
        } catch (Exception e) {
            //系统异常 可能异常信息里有敏感信息，不建议给用户看。丢人。
            e.printStackTrace();
            return ResponseDTO.fail("系统出错啦，请联系客服！");
        }
    }

    /**
     *
     * @param cardId
     * @param password
     * @param currentPage 通过RequestParam标签，可以对参数进行必填设置，也可以起个别名。注意：容错性。
     * @return
     */
    @PostMapping("/card/queryFlow")
    public ResponseDTO queryFlow(long cardId,
                                 @RequestParam(name = "pwd") String password,
                                 @RequestParam(required = false, defaultValue = "1") int currentPage) {
        try {
//            return ResponseDTO.success(cardService.queryFlow(cardId, password, currentPage));
            return ResponseDTO.success(cardService.queryFlow2(cardId, password, currentPage));
        } catch (BizException be) {
            be.printStackTrace();
            return ResponseDTO.fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.fail("系统出错啦，请联系客服！");
        }
    }

    @RequestMapping(value = "/card/queryMyCard", method = RequestMethod.GET)
    public ResponseDTO queryMyCard(HttpSession session) {
        return ResponseDTO.success(cardService.queryMyCards(getUserId(session)));
    }


    @GetMapping("/card/queryFlowTop10")
    public ResponseDTO queryFlowTop10(HttpSession session) {
        return ResponseDTO.success(cardService.queryFlowTop10(getUserId(session)));
    }


}
